home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / CC_C / 0696A.ZIP / TIMEDAT2.C < prev    next >
Text File  |  1987-05-25  |  2KB  |  96 lines

  1. /**
  2. *  timedat2
  3. *          
  4. * check env if not found TZ variable add it....
  5. **/
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <spstd.h>
  10. #include <time.h>
  11. main(argc, argv, envp)
  12. int argc;
  13. char **argv;
  14. char **envp;
  15. {
  16.     register char *ep;
  17.         static char timestr[] = { "TZ" };
  18.     static char pgm[MAXNAME + 1] = { "showenv" };
  19.     extern void getpname(char *, char *);
  20.         extern void settz();
  21.         extern void telltime();
  22.      /* use an alias if one given to this program */
  23.     if (_osmajor >= 3)
  24.         getpname(*argv, pgm);
  25. /* if no arguments  show the full environment list */
  26. /*
  27. * treat all args as DOS variable names and display values of
  28. * only specified variables
  29. */
  30.           if ((ep = getenv(strupr(timestr))) == NULL)
  31.            {
  32.             fprintf(stderr, "%s not defined\n", timestr);
  33.             fprintf(stderr, "Adding set TZ=EST5EDT\n");
  34.                         settz();
  35.                         
  36.             }
  37.           else
  38.             printf("%s=%s\n", timestr, ep);
  39. telltime();
  40.     exit(0);
  41. }
  42. void settz()
  43. {
  44.     register char **p;
  45.     static char tzstring[] = { "TZ" };
  46.     static char pgm[MAXNAME + 1]= { "setmydir" };
  47.     extern  void getpname(char *, char *);
  48.         extern void err_exit(char *, char *,int);
  49.     
  50. /* try to add TZ variable to environment */
  51. if (putenv("TZ=EST5EDT") == -1)
  52.     err_exit(pgm, "ERROR CHANGING ENVIRONMENT", 1);
  53. /* display the environment for this process */
  54. for (p = environ; *p; p++) {
  55.     printf("%s\n", *p);
  56. }
  57. return(0);
  58. }
  59.  
  60.  
  61.  
  62. void err_exit(pname, str, errlvl)
  63. char *pname;
  64. char *str;
  65. int errlvl;
  66.  
  67.     fputs(pname, stderr);
  68.     fputc(':',stderr);
  69.     fputc(' ',stderr);
  70. /* for fatal add perror(str); */
  71.     exit(errlvl);
  72. }
  73. void telltime()
  74. {
  75.     long now;
  76.     struct tm *tbuf;
  77. /* get TZ data into global variables */
  78. tzset();
  79. /* display the global time values */
  80. printf("daylight savings time flag = %d\n", daylight);
  81. printf("difference (in seconds) from GMT = %ld\n", timezone);
  82. printf("standard time zone string is %s\n", tzname[0]);
  83. printf("daylight time zone string is %s\n", tzname[1]);
  84.  
  85. /**
  86. * display the current date and time values for local and universal time
  87. **/
  88. now = time(NULL);
  89. printf("\nctime():\t%s\n", ctime(&now));
  90. tbuf = localtime(&now);
  91. printf("local time:\t%s\n", asctime(tbuf));
  92. tbuf = gmtime(&now);
  93. printf("universal time:\t%s\n", asctime(tbuf));
  94. return(0);
  95. }